package com.xunshangwang.pinhaohuo.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.xunshangwang.pinhaohuo.R;


/**
 * Created by xzp on 15/12/8.
 */
public class RatioImageView extends ImageView{
    float mRatio;
    public RatioImageView(Context context) {
        super(context);
        initRatio(context,null);
    }

    public RatioImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRatio(context,attrs);
    }

    public RatioImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initRatio(context,attrs);
    }
    void initRatio(Context context,AttributeSet attrs){
        if(attrs==null)
            mRatio=0;
        else {
            TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.RatioImageView);
            mRatio=array.getFloat(R.styleable.RatioImageView_ratio,0);
            array.recycle();
        }
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(mRatio>0){
            setMeasuredDimension(getMeasuredWidth(), (int) (getMeasuredWidth()*mRatio));
        }
    }
}
